// 全局加载状态管理工具
class LoadingManager {
  constructor() {
    this.loadingInstances = new Map()
    this.globalLoading = null
  }

  // 注册加载组件实例
  registerLoading(id, instance) {
    this.loadingInstances.set(id, instance)
  }

  // 注销加载组件实例
  unregisterLoading(id) {
    this.loadingInstances.delete(id)
  }

  // 设置全局加载实例
  setGlobalLoading(instance) {
    this.globalLoading = instance
  }

  // 显示加载状态
  showLoading(options = {}) {
    const {
      id = 'default',
      skeletonType = 'default',
      loadingText = '加载中...',
      skeletonDelay = 300,
      customClass = '',
      useGlobal = false
    } = options

    const loadingOptions = {
      skeletonType,
      loadingText,
      skeletonDelay,
      customClass
    }

    if (useGlobal && this.globalLoading) {
      this.globalLoading.showLoading(loadingOptions)
    } else {
      const instance = this.loadingInstances.get(id)
      if (instance) {
        instance.showLoading(loadingOptions)
      }
    }
  }

  // 隐藏加载状态
  hideLoading(id = 'default', useGlobal = false) {
    if (useGlobal && this.globalLoading) {
      this.globalLoading.hideLoading()
    } else {
      const instance = this.loadingInstances.get(id)
      if (instance) {
        instance.hideLoading()
      }
    }
  }

  // 显示成功提示
  showSuccess(message = '操作成功', id = 'default', useGlobal = false) {
    if (useGlobal && this.globalLoading) {
      this.globalLoading.showSuccess(message)
    } else {
      const instance = this.loadingInstances.get(id)
      if (instance) {
        instance.showSuccess(message)
      }
    }
  }

  // 显示错误提示
  showError(message = '操作失败', id = 'default', useGlobal = false) {
    if (useGlobal && this.globalLoading) {
      this.globalLoading.showError(message)
    } else {
      const instance = this.loadingInstances.get(id)
      if (instance) {
        instance.showError(message)
      }
    }
  }

  // 包装异步函数，自动处理加载状态
  async wrapAsync(fn, options = {}) {
    const {
      id = 'default',
      skeletonType = 'default',
      loadingText = '加载中...',
      successMessage = '操作成功',
      errorMessage = '操作失败',
      showSuccess = false,
      useGlobal = false
    } = options

    try {
      this.showLoading({
        id,
        skeletonType,
        loadingText,
        useGlobal
      })

      const result = await fn()
      
      if (showSuccess) {
        this.showSuccess(successMessage, id, useGlobal)
      }

      return result
    } catch (error) {
      console.error('异步操作失败:', error)
      this.showError(errorMessage, id, useGlobal)
      throw error
    } finally {
      this.hideLoading(id, useGlobal)
    }
  }

  // 包装API请求，自动处理加载状态
  async wrapApiRequest(apiCall, options = {}) {
    const {
      id = 'default',
      skeletonType = 'default',
      loadingText = '请求中...',
      successMessage = '请求成功',
      errorMessage = '请求失败',
      showSuccess = false,
      useGlobal = false
    } = options

    return this.wrapAsync(async () => {
      const result = await apiCall()
      
      if (!result.success) {
        throw new Error(result.message || errorMessage)
      }
      
      return result
    }, {
      id,
      skeletonType,
      loadingText,
      successMessage,
      errorMessage,
      showSuccess,
      useGlobal
    })
  }
}

// 创建全局实例
const loadingManager = new LoadingManager()

// 导出工具函数
export const showLoading = (options) => loadingManager.showLoading(options)
export const hideLoading = (id, useGlobal) => loadingManager.hideLoading(id, useGlobal)
export const showSuccess = (message, id, useGlobal) => loadingManager.showSuccess(message, id, useGlobal)
export const showError = (message, id, useGlobal) => loadingManager.showError(message, id, useGlobal)
export const wrapAsync = (fn, options) => loadingManager.wrapAsync(fn, options)
export const wrapApiRequest = (apiCall, options) => loadingManager.wrapApiRequest(apiCall, options)

// 导出管理器实例
export default loadingManager
